var area;
$(function(){
	init();
	initList();
	initButton();
	initBottom();
});
//进入页面的初始化
function init(){
	var index_location = localStorage.getItem("index-location");
	if(index_location){
		$("#expressArea").html(index_location);
		area = index_location;
	}
}
function initList(){
	//获取热门推荐
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaHotList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			area:area
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var productList = result.data;
				if(productList.length==0){
					if(area!=''){
						area = '';
						initList();
					}
				}
				var str = '';
				$.each(productList,function(i,n){
					str+='<div class="swiper-slide" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'"/><div class="img-det clear"><span class="fl">'+n.teaTitle+'</span><span class="fr">'+n.legalName+":"+n.teaPhone+'</span></div></div>';
				});
				$('.swiper-wrapper').html(str);
				var mySwiper1 = new Swiper('.swiper-container2', {
					direction: 'horizontal',
					loop: true,
				});
			}else{
				showErrorMsg(result.data);
			}
		}
	});
	//获取列表
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaGardenList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			area:area
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var productList = result.data;
				if(productList.length==0){
					if(area!=''){
						area = '';
						initList();
					}
				}
				var str = '';
				$.each(productList,function(i,n){
					var gandenTypeStr = '';
					if(n.gandenType==0){
						gandenTypeStr = "国家生态林";
					}else if(n.gandenType==1){
						gandenTypeStr = "成型茶园";
					}else if(n.gandenType==2){
						gandenTypeStr = "荒山茶林";
					}/*<span class="fl color02">转让</span>*/
					var rengStr = '';
					if(n.rentType==0){
						//整租
						rentStr = '<span class="fl color02">整租</span>'
					}else{
						//分租
						rentStr = '<span class="fl color03">分租</span>'
					}
					str +=' <div class="list-item clear" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="item-r fl"><p class="item-tit">'+n.teaTitle+'</p><div class="item-det clear"><span class="fl color01">'+gandenTypeStr+'</span>'+rentStr+'<span class="fl color03">已认证</span></div><div class="item-time clear"><p class="fl">'+n.teaArea+'亩</p><span class="fr">'+n.limitTime+'年</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>元/亩·年</div><p class="fr">'+n.teaLocation+'</p></div></div></div>';
				});
				$('.index-list').html(str);
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}
//初始化页面按钮绑定事件
function initButton(){
	//绑定进入明细事件
	$('body').on('click','.list-item',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	
	$('body').on('click','.swiper-slide',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	$("#organ").bind('click',function(){
		window.location=server_url+"organPro.do";
	});
}
//绑定下列表切换
function initBottom(){
	//跳转到首页
	$("#index").bind('click',function(){
		window.location=server_url+"buyer/product/index.do";
	});
	//跳转到我的
	$("#tea-my").bind('click', function() {
		window.location = server_url + "teaMy.do";
	});
	//跳转到头条咨询
	$("#inf").bind('click', function() {
		window.location = server_url + "inf.do";
	});
	//跳转到发布
	$(".demand-btn").bind('click', function() {
		window.location = server_url + "release.do";
	});
}
//改变地区，重新根据地区进行过滤
function changeArea(areaP){
	area = areaP;
	localStorage.setItem("index-location",areaP);
	initList();
}